package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.DefinedView.MyAlertDialog;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.view.entrance.fragment.FindPasswordFragment;
import com.monkeygroup.mygoplus.view.entrance.fragment.SetPasswordFragment;

import java.util.HashMap;
import java.util.Map;

/**
 * 輸入驗證碼
 * Created by cumt on 2017/6/28.
 */

public class PersonInputCodeActivity extends BaseActivity implements View.OnClickListener{


    public static void gotoActivity(Activity activity,String phone) {
        Intent intent = new Intent(activity,
                PersonInputCodeActivity.class);

        intent.putExtra("phone",phone);
        activity.startActivityForResult(intent,300);
    }

    private Activity activity;

    private ImageView imgBack;
    private TextView txtTitle;

    private EditText et_code_I,et_code_II,et_code_III,et_code_IIII,et_code_IIIII;

    private TextView code_time,txt_publish_content,btn_change_state_ok;
    private TimeCount time;

    //請求參數
    private Map<String, String> params;
    private HttpModel checkVerify,httpModel,changeHttpModel;;

    private String phone;
    private StringBuffer code;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_input_state);

    }

    @Override
    public void initView() {

        activity= PersonInputCodeActivity.this;

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("請輸入驗證碼");

        txt_publish_content= (TextView) findViewById(R.id.txt_publish_content);
        btn_change_state_ok= (TextView) findViewById(R.id.btn_change_state_ok);

        et_code_I= (EditText) findViewById(R.id.et_code_I);
        et_code_II= (EditText) findViewById(R.id.et_code_II);
        et_code_III= (EditText) findViewById(R.id.et_code_III);
        et_code_IIII= (EditText) findViewById(R.id.et_code_IIII);
        et_code_IIIII= (EditText) findViewById(R.id.et_code_IIIII);

        code_time= (TextView)findViewById(R.id.code_time);
        time = new TimeCount(30000, 1000);//构造CountDownTimer对象

        time.start();
        Intent intent = this.getIntent();
        Bundle bundle =  intent.getExtras();
        phone= bundle.getString("phone");
        txt_publish_content.setText(phone);

        code = new StringBuffer("");
        params=new HashMap<>();
        params.put("mobile",phone);
        initWatcher();
        initRequest();
    }

    @Override
    public void setListener() {

    }

    @Override
    public void doEvent() {

    }

    private void initWatcher(){

        et_code_I.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                code.append(String.valueOf(et_code_I.getText()));
                if(code.length()==6){
                    params.put("verifyCode",code.toString());
                    checkVerify.startRequest();
                }

            }
        });
        et_code_II.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                code.append(String.valueOf(et_code_II.getText()));
                if(code.length()==6){
                    params.put("verifyCode",code.toString());
                    checkVerify.startRequest();
                }


            }
        });
        et_code_III.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                code.append(String.valueOf(et_code_III.getText()));
                if(code.length()==6){
                    params.put("verifyCode",code.toString());
                    checkVerify.startRequest();
                }

            }
        });
        et_code_IIII.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                code.append(String.valueOf(et_code_IIII.getText()));
                if(code.length()==6){
                    params.put("verifyCode",code.toString());
                    checkVerify.startRequest();
                }

            }
        });
        et_code_IIIII.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                code.append(String.valueOf(et_code_IIIII.getText()));
                if(code.length()==6){
                    params.put("verifyCode",code.toString());

                    checkVerify.startRequest();
                }
                else{
                    Toast.makeText(activity, "請輸入驗證碼",
                            Toast.LENGTH_SHORT).show();
                }


            }
        });


    }

    private void initRequest(){

        //发送短信
        httpModel = new HttpModel(RequestUrl.baseUrl + RequestUrl.verifyUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if (!VilidateUtil.isSuccess(resultCode)) {

//                    MyAlertDialog dialog = new MyAlertDialog(activity, resultMsg, 0);
//                    dialog.show(activity, resultMsg, 0);

                    //  code=(String)data;
                } else {
                    time.start();


                }
            }
        }, true);

        //短信验证
        checkVerify= new HttpModel(RequestUrl.baseUrl + RequestUrl.checkVerifyUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if (!VilidateUtil.isSuccess(resultCode)) {

//                    MyAlertDialog dialog = new MyAlertDialog(activity, resultMsg, 0);
//                    dialog.show(activity, resultMsg, 0);

                } else {
                    Map<String,String> result = (HashMap)data;
                    if(result!=null && result.get("message").equals("验证码输入正确")){
                        btn_change_state_ok.setText("變更手機號");
                        btn_change_state_ok.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                changeHttpModel.startRequest();
                            }
                        });
                    }
                    else if(result!=null && result.get("message").equals("验证码输入错误")){
                        MyAlertDialog dialog = new MyAlertDialog(activity, "验证码输入错误", "");
                        dialog.show(activity, "验证码输入错误", 3);

                    }
                    else if(result!=null && result.get("message").equals("验证码失效")){
                        MyAlertDialog dialog = new MyAlertDialog(activity, "验证码失效", "");
                        dialog.show(activity, "验证码失效", 3);

                    }

                }
            }
        }, true);


        changeHttpModel = new HttpModel(RequestUrl.baseUrl+RequestUrl.updateUserUrl, params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if (VilidateUtil.isSuccess(resultCode)) {

                    final Intent intent=new Intent();
                    intent.putExtra("phone", String.valueOf(params.get("phone")));
                    Toast.makeText(activity,"修改成功", Toast.LENGTH_SHORT).show();
                    activity.setResult(RESULT_OK,intent);
                    finish();
                }
                else{
                    Toast.makeText(activity,"修改失败", Toast.LENGTH_SHORT).show();
                }

            }
        },true);
    }


    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }
        @Override
        public void onFinish() {//计时完毕时触发
//            code_time.setText("重新验证");
//            code_time.setClickable(true);
//            code_time.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    httpModel.startRequest();
//
//                }
//            });
            btn_change_state_ok.setBackgroundResource(R.drawable.btn_red_shape);

            //  code_time.setTextColor(getResources().getColor(R.color.font_gray_III));
        }
        @Override
        public void onTick(long millisUntilFinished){//计时过程显示
            code_time.setClickable(false);
            code_time.setText(millisUntilFinished / 1000 + "秒");
            // code_time.setTextColor(getResources().getColor(R.color.font_black));
        }
    }
}
